local chihua = fk.CreateSkill {
  name = "emo__chihua",
}

chihua:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(chihua.name) and player.phase == Player.Play and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askToDiscard(player, {
      min_num = 1, max_num = 999, include_equip = true, cancelable = true, skill_name = chihua.name, skip =  true,
      prompt = "#emo__chihua-prompt"
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    local n = 0
    for _, id in ipairs(cards) do
      if Fk:getCardById(id).type ~= Card.TypeBasic then
        n = n + 1
      end
    end
    room:throwCard(cards, chihua.name, player, player)
    if player.dead then return end
    if n > 0 then
      player:drawCards(n, chihua.name)
      if player.dead then return end
    end
    n = n + #cards
    local list = {"slash","jink","analeptic","peach","slashZLeffect","jinkZLeffect","analepticZLeffect","peachZLeffect"}
    local mark = player:getTableMark("@$emo__chihua")
    local names = table.filter(list, function(s) return not table.contains(mark, s) end)
    if #names == 0 then return end
    table.insertTable(mark, table.random(names, n))
    room:setPlayerMark(player, "@$emo__chihua", mark)
  end,
})

chihua:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if not player.dead and target == player then
      local mark = player:getTableMark("@$emo__chihua")
      local name = (data.extra_data or Util.DummyTable).emo__moyao_name
      if name then
        return table.contains(mark, name) or table.contains(mark, name.."ZLeffect")
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = data.extra_data.emo__moyao_name
    local mark = player:getTableMark("@$emo__chihua")
    if table.contains(mark, name) then
      player:drawCards(1, chihua.name)
    end
    if name == "jink" and table.contains(mark, "jinkZLeffect") then
      if not player.dead and data.responseToEvent then
        local to = data.responseToEvent.from
        if to and not to:isNude() then
          local cid = room:askToChooseCard(player, {target = to, skill_name = chihua.name, flag = "he"})
          room:obtainCard(player, cid, false, fk.ReasonPrey, player, chihua.name)
        end
      end
    elseif name == "analeptic" and table.contains(mark, "analepticZLeffect") then
      data.extra_data = data.extra_data or {}
      data.extra_data.additionalDrank = (data.extra_data.additionalDrank or 0) + 1
    elseif name == "peach" and table.contains(mark, "peachZLeffect") then
      data.additionalRecover = (data.additionalRecover or 0) + 1
    end
  end,
})

-- 给魔药的杀和闪加标记
chihua:addEffect(fk.PreCardUse, {
  can_refresh = function(self, event, target, player, data)
    if target == player and data.card:getMark("@@emo__moyao-inhand") > 0 then
      local cards = data.card.subcards
      return #cards == 0 or (#cards == 1 and Fk:getCardById(cards[1]).trueName == data.card.trueName)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.emo__moyao_name = data.card.trueName
  end,
})

-- 给魔药的杀加无双效果
chihua:addEffect(fk.TargetSpecified, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      local mark = player:getTableMark("@$emo__chihua")
      local name = (data.extra_data or Util.DummyTable).emo__moyao_name
      if name == "slash" then
        return table.contains(mark, name) and table.contains(mark, name.."ZLeffect")
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    data:setResponseTimes(data:getResponseTimes() + 1)
  end,
})



Fk:loadTranslationTable{
  ["emo__chihua"] = "赤化",
  [":emo__chihua"] = "出牌阶段开始时，你可以弃置任意张牌，随机升级等量次数“魔药”中基本牌的效果（每弃置一张非基本牌则额外升级一次并摸一张牌）："..
  "<br>①—④【酒】/【桃】/【杀】/【闪】使用时摸一张牌；<br>⑤【酒】影响的牌造成伤害+1；<br>⑥【桃】回复值+1；<br>⑦【杀】抵消需要的牌数+1；<br>⑧【闪】获得响应角色一张牌。",

  ["@$emo__chihua"] = "赤化",
  ["#emo__chihua-prompt"] = "赤化：你可以弃置任意张牌，升级“魔药”！若弃置非基本牌，则摸等量张牌",
}


return chihua
